package com.rogue.blog.service.article.impl;

import com.alibaba.fastjson.JSON;
import com.rogue.blog.dao.article.ArticleCategoryDao;
import com.rogue.blog.model.dto.article.ArticleCategoryDTO;
import com.rogue.blog.model.po.PageBean;
import com.rogue.blog.model.po.PageCondition;
import com.rogue.blog.model.po.article.ArticleCategoryPO;
import com.rogue.blog.model.vo.article.ArticleCategoryVO;
import com.rogue.blog.service.article.ArticleCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 86101 on 2018-10-09.
 */
@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    // TODO
    private long userId = 4137048204575744L;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ArticleCategoryDao articleCategoryDao;

    @Override
    public long insert(ArticleCategoryDTO dto) {
        List<ArticleCategoryPO> pos = articleCategoryDao.selectCategoryByNameAndUserId(dto.getCategoryName(), userId);
        long categoryId = 0;
        if (CollectionUtils.isEmpty(pos)) {
            ArticleCategoryPO po = dto.toPO();
            categoryId = restTemplate.getForObject("http://rogue-generator-service/nextId", Long.class);
            po.setCategoryId(categoryId);
            po.setUserId(userId);
            po.setCreateUserId(userId);
            po.setUpdateUserId(userId);
            int row = articleCategoryDao.insert(po);
            if (0 == row) {
                categoryId = 0;
            }
        } else {
            categoryId = pos.get(0).getCategoryId();
        }
        return categoryId;
    }

    @Override
    public int deleteById(long id, boolean hardDelete) {
        ArticleCategoryPO po = articleCategoryDao.selectById(id);
        if (userId == po.getUserId()) {
            if (hardDelete) {
                return articleCategoryDao.deleteById(id);
            } else {
                po.setValid("0");
                po.setUpdateUserId(userId);
                return articleCategoryDao.updateById(po);
            }
        } else {
            return 0;
        }
    }

    @Override
    public int updateById(long id, ArticleCategoryDTO dto) {
        ArticleCategoryPO po = articleCategoryDao.selectById(id);
        if (null == po) {
            return 0;
        } else {
            po = dto.toPO();
            po.setCategoryId(id);
            po.setUpdateUserId(userId);
            return articleCategoryDao.updateById(po);
        }
    }

    @Override
    public ArticleCategoryVO selectById(long id) {
        ArticleCategoryPO po = articleCategoryDao.selectById(id);
        ArticleCategoryVO vo = null;
        if (null != po) {
            vo = po.toVO();
        }
        return vo;
    }

    @Override
    public PageBean<ArticleCategoryVO> selectByPage(PageCondition pageCondition) {
        Integer currentPage = pageCondition.getCurrentPage();
        Integer pageSize = pageCondition.getPageSize();
        int start = (currentPage - 1) * pageSize;
        Map<String, Object> pageMap = JSON.parseObject(JSON.toJSONString(pageCondition), Map.class);
        pageMap.put("userId", userId);
        pageMap.put("start", start);
        pageMap.put("limit", pageSize);
        List<ArticleCategoryPO> pos = articleCategoryDao.selectByPage(pageMap);
        int totalCount = articleCategoryDao.selectCountByPage(pageMap);
        List<ArticleCategoryVO> dtos = new ArrayList<>();
        for (ArticleCategoryPO po : pos) {
            dtos.add(po.toVO());
        }
        PageBean<ArticleCategoryVO> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setPageData(dtos);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public boolean exists(String categoryName) {
        return !CollectionUtils.isEmpty(articleCategoryDao.selectCategoryByNameAndUserId(categoryName, userId));
    }
}
